package com.Android.jizhangla;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Login extends Activity {

	private EditText loginview_username;
	private EditText loginview_password;
	private Button loginview_bt_login;

	private String userName;
	private String password;
	private String URL;

	private Boolean loginStates;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);

		// 获取控件对象
		loginview_username = (EditText) findViewById(R.id.et_username);
		loginview_password = (EditText) findViewById(R.id.et_password);
		loginview_bt_login = (Button) findViewById(R.id.bt_login);

		// 登录按钮绑定监听事件
		loginview_bt_login.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO 自动生成的方法存根
				Log.v("登录按钮", "点击登录按钮");

				userName = loginview_username.getText().toString();
				password = loginview_password.getText().toString();
				URL = "http://wap.jizhangla.com/login/index.aspx?post=Login";

				if (validateLocalLogin(userName, password, URL)) {
					Log.v("登录结果", "登录成功！");
					// 登录成功，载入index Activity布局
					Intent intent = new Intent();
					intent.setClass(Login.this, index.class);
					startActivity(intent);
					
				} else {
					Log.v("登录结果", "登录失败");

					Toast.makeText(Login.this, "用户名或密码错误", Toast.LENGTH_SHORT)
							.show();
				}
			}
		});

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_login, menu);
		return true;
	}

	/**
	 * 检查用户登陆 如果返回的数据里包含list.aspx字符串，表示登录成功 如果连接服务器超过5秒,也算连接失败.
	 * 
	 * @param userName
	 *            用户名
	 * @param password
	 *            密码
	 * @param URL
	 *            检查登陆的地址
	 * */

	private Boolean validateLocalLogin(String userName, String password,
			String URL) {
		// 登录成功与否判定
		loginStates = false;
		// 建立POST对象
		HttpPost httpRequest = new HttpPost(URL);
		// 封装POST参数
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("txlUserName", userName));
		params.add(new BasicNameValuePair("txlPassWord", password));
		params.add(new BasicNameValuePair("login", "登录"));

		try {
			// 添加请求参数到请求对象
			httpRequest.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
			// 发送请求并等待相应
			HttpResponse httpResponse = new DefaultHttpClient()
					.execute(httpRequest);
			Integer i = httpResponse.getStatusLine().getStatusCode();
			Log.v("状态码", i.toString());
			if (httpResponse.getStatusLine().getStatusCode() > 0) {
				// 读取返回数据
				String strResult = EntityUtils.toString(httpResponse
						.getEntity());
				System.out.println(strResult);
				Log.v("strResult", strResult);

				System.out.println("---------      "
						+ strResult.indexOf("list.aspx"));

				if (strResult.indexOf("list.aspx") > 0) {
					Log.v("登录过程", "登录验证成功");
					loginStates = true;
				} else if (strResult.matches("go")) {//

					Log.v("登录过程", "登录验证失败");
				} else {
					Log.v("失败", "失败");
				}

				// 登录返回信息后结束提示框

			} else {

				httpResponse.getStatusLine().toString();
			}
		} catch (ClientProtocolException e) {
			Log.d(this.toString(), "ClientProtocolException is error");

			e.printStackTrace();
		} catch (IOException e) {
			Log.d(this.toString(), "IOException");

			e.printStackTrace();
		} catch (Exception e) {

			e.printStackTrace();
		}
		return loginStates;
	}
}
